package com.syn.synold4s.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.result.ActivityResult;

import com.gaoyi.basemodule.baseAdapter.BasePagerAdapter;
import com.gaoyi.basemodule.baseUtil.WindowUtil;
import com.gaoyi.basemodule.baseact.BaseViewActivity;
import com.gaoyi.basemodule.basebean.Event;
import com.syn.synold4s.R;
import com.syn.synold4s.activity.base.BaseAlertViewActivity;
import com.syn.synold4s.adapter.ImagePreviewAdapter;
import com.syn.synold4s.databinding.ActivityImagePreviewBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ImagePreviewActivity extends BaseAlertViewActivity<ActivityImagePreviewBinding> {

    public static final String DATA = "data";
    public static final String ID = "id";
    private List<String> imageList;
    private int mCurrentPosition, mSize;

    public static void photoPreview(Context context, ArrayList<String> listPhotos, int position) {
        Intent intent = new Intent(context, ImagePreviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putStringArrayList(ImagePreviewActivity.DATA, listPhotos);
        bundle.putInt(ImagePreviewActivity.ID, position);
        intent.putExtra(ImagePreviewActivity.DATA, bundle);
        context.startActivity(intent);
    }

    @Override
    protected void initData() {
        WindowUtil.setWindowDecorView(this);
        mBinding.ivClose.setOnClickListener(v -> finish());
        getIntentData();
        setView();
    }

    private void getIntentData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra(DATA);
        if (bundle != null) {
            imageList = bundle.getStringArrayList(DATA);
            mCurrentPosition = bundle.getInt(ID, 0);
        }
    }

    private void setView() {
        if (imageList == null) {
            return;
        }
        mSize = imageList.size();
        setNumShow();
        ImagePreviewAdapter adapter = new ImagePreviewAdapter(this, imageList);
        adapter.setOnItemClickListener((v, position) -> {
            finish();
        });
        mBinding.vpImage.setAdapter(adapter);
        mBinding.vpImage.setCurrentItem(mCurrentPosition);
        mBinding.vpImage.addOnPageChangeListener(new androidx.viewpager.widget.ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurrentPosition = position;
                setNumShow();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mBinding.vpImage.setPageTransformer(false, (page, position) -> {
            float normalizedPosition = Math.abs(Math.abs(position) - 1);
            page.setScaleX(normalizedPosition / 2 + 0.5f);
            page.setScaleY(normalizedPosition / 2 + 0.5f);
        });
    }

    private void setNumShow() {
        mBinding.tvNum.setText(getString(R.string.img_view, mCurrentPosition + 1, mSize));
    }

    @Override
    protected ActivityImagePreviewBinding getViewBinding() {
        return ActivityImagePreviewBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }
}